Skip to main content

Lesson 3: Methods in Java: Defining, Invoking, and Using Methods Effectively

In this lesson, you'll learn how to define and use methods effectively in Java. Methods are reusable blocks of code that help you organize and structure your programs. Understanding how to define, invoke, and pass data to methods is a crucial skill for Java developers.


1. What Are Methods?

A method is a block of code designed to perform a specific task. Methods are used to:

  • Improve code reusability.
  • Simplify program structure.
  • Break complex problems into smaller, manageable parts.

2. Defining Methods

A method definition has the following structure:

accessModifier returnType methodName(parameters) {
// Method body
return value; // Optional (if the returnType is not void)
}
  • accessModifier: Defines the visibility of the method (e.g., public, private).
  • returnType: Specifies the type of value the method returns (e.g., int, double, void).
  • methodName: Name of the method (should be descriptive).
  • parameters: Optional variables passed to the method.

3. Invoking Methods

To use a method, you invoke it using its name followed by parentheses, optionally passing arguments.


Code Examples

Example 1: A Simple Method

public class SimpleMethod {
// Method definition
public static void sayHello() {
System.out.println("Hello, World!");
}

public static void main(String[] args) {
// Method invocation
sayHello();
}
}

Expected Output:

Hello, World!

Example 2: Method with Parameters

public class MethodWithParameters {
// Method definition with parameters
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}

public static void main(String[] args) {
// Method invocation with an argument
greet("Alice");
}
}

Expected Output:

Hello, Alice!

Example 3: Method with a Return Value

public class MethodWithReturn {
// Method definition that returns a value
public static int addNumbers(int a, int b) {
return a + b;
}

public static void main(String[] args) {
int sum = addNumbers(5, 10); // Capture the return value
System.out.println("The sum is: " + sum);
}
}

Expected Output:

The sum is: 15

4. Overloading Methods

Method overloading allows you to define multiple methods with the same name but different parameter lists. Java differentiates them by their number or type of parameters.

Example: Overloaded Methods

public class MethodOverloading {
// Method with one parameter
public static void display(int number) {
System.out.println("Number: " + number);
}

// Method with two parameters
public static void display(int number, String message) {
System.out.println(message + number);
}

public static void main(String[] args) {
display(5);
display(10, "The value is: ");
}
}

Expected Output:

Number: 5
The value is: 10

5. Advanced: Recursive Methods

A recursive method is a method that calls itself. Recursion is useful for solving problems like factorial calculation, Fibonacci series, or traversing trees.

Example: Factorial Using Recursion

public class FactorialRecursion {
// Recursive method
public static int factorial(int n) {
if (n == 0) {
return 1; // Base case
}
return n * factorial(n - 1); // Recursive call
}

public static void main(String[] args) {
int result = factorial(5);
System.out.println("Factorial of 5 is: " + result);
}
}

Expected Output:

Factorial of 5 is: 120